parsewebidl.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. program parsewebidl;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, webidlparser, webidlscanner,webidldefs;
  5. ResourceString
  6. SErrNeedInputFiles = 'Need one or more input files';
  7. type
  8. { TParseWebIDLApplication }
  9. TParseWebIDLApplication = class(TCustomApplication)
  10. private
  11. FContext : TWebIDLContext;
  12. procedure ParseWebIDL(const AFileName: String);
  13. protected
  14. procedure DoRun; override;
  15. public
  16. constructor Create(TheOwner: TComponent); override;
  17. destructor Destroy; override;
  18. procedure WriteHelp(Const Msg : String); virtual;
  19. end;
  20. { TParseWebIDLApplication }
  21. procedure TParseWebIDLApplication.ParseWebIDL(Const AFileName : String);
  22. Var
  23. F : TFileStream;
  24. P : TWebIDLParser;
  25. S : TWebIDLScanner;
  26. I : Integer;
  27. begin
  28. FreeAndNil(FContext);
  29. FContext:=TWebIDLContext.Create;
  30. P:=Nil;
  31. S:=Nil;
  32. F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
  33. try
  34. S:=TWebIDLScanner.Create(F);
  35. P:=TWebIDLParser.Create(FContext,S);
  36. P.Parse;
  37. Writeln('// Contents of '+AFileName);
  38. For I:=0 to FConText.Definitions.Count-1 do
  39. begin
  40. Writeln('// Definition ',I+1:3,': ',FConText.Definitions[i].ClassName);
  41. Writeln(FConText.Definitions[i].AsString(True)+';');
  42. end;
  43. finally
  44. F.Free;
  45. P.Free;
  46. S.Free;
  47. end;
  48. end;
  49. procedure TParseWebIDLApplication.DoRun;
  50. var
  51. FN,ErrorMsg: UTF8String;
  52. NoF : TStringArray;
  53. begin
  54. Terminate;
  55. ErrorMsg:=CheckOptions('hi:', ['help','input']);
  56. if (ErrorMsg<>'') or HasOption('h','help') then
  57. begin
  58. WriteHelp(ErrorMsg);
  59. Exit;
  60. end;
  61. FN:=GetOptionValue('i','input');
  62. if FN='' then
  63. NoF:=GetNonOptions('hi:', ['help','input'])
  64. else
  65. begin
  66. SetLength(NOF,1);
  67. NOF[0]:=FN;
  68. end;
  69. if Length(Nof)=0 then
  70. WriteHelp(SErrNeedInputFiles);
  71. For FN in NoF do
  72. ParseWebIDL(FN);
  73. end;
  74. constructor TParseWebIDLApplication.Create(TheOwner: TComponent);
  75. begin
  76. inherited Create(TheOwner);
  77. StopOnException:=True;
  78. FContext:=TWebIDLContext.Create;
  79. end;
  80. destructor TParseWebIDLApplication.Destroy;
  81. begin
  82. FreeAndNil(FContext);
  83. inherited Destroy;
  84. end;
  85. procedure TParseWebIDLApplication.WriteHelp(Const Msg : String);
  86. begin
  87. if Msg<>'' then
  88. Writeln('Error : ',Msg);
  89. writeln('Usage: ', ExeName, ' -h');
  90. ExitCode:=Ord(Msg<>'');
  91. end;
  92. var
  93. Application: TParseWebIDLApplication;
  94. begin
  95. Application:=TParseWebIDLApplication.Create(nil);
  96. Application.Title:='Parse WEB IDL Application';
  97. Application.Run;
  98. Application.Free;
  99. end.