parsewebidl.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.Version:=v2;
  37. P.Parse;
  38. Writeln('// Contents of '+AFileName);
  39. For I:=0 to FConText.Definitions.Count-1 do
  40. begin
  41. Writeln('// Definition ',I+1:3,': ',FConText.Definitions[i].ClassName);
  42. Writeln(FConText.Definitions[i].AsString(True)+';');
  43. end;
  44. finally
  45. F.Free;
  46. P.Free;
  47. S.Free;
  48. end;
  49. end;
  50. procedure TParseWebIDLApplication.DoRun;
  51. var
  52. FN,ErrorMsg: UTF8String;
  53. NoF : TStringArray;
  54. begin
  55. Terminate;
  56. ErrorMsg:=CheckOptions('hi:', ['help','input']);
  57. if (ErrorMsg<>'') or HasOption('h','help') then
  58. begin
  59. WriteHelp(ErrorMsg);
  60. Exit;
  61. end;
  62. FN:=GetOptionValue('i','input');
  63. if FN='' then
  64. NoF:=GetNonOptions('hi:', ['help','input'])
  65. else
  66. begin
  67. SetLength(NOF,1);
  68. NOF[0]:=FN;
  69. end;
  70. if Length(Nof)=0 then
  71. WriteHelp(SErrNeedInputFiles);
  72. For FN in NoF do
  73. ParseWebIDL(FN);
  74. end;
  75. constructor TParseWebIDLApplication.Create(TheOwner: TComponent);
  76. begin
  77. inherited Create(TheOwner);
  78. StopOnException:=True;
  79. FContext:=TWebIDLContext.Create;
  80. end;
  81. destructor TParseWebIDLApplication.Destroy;
  82. begin
  83. FreeAndNil(FContext);
  84. inherited Destroy;
  85. end;
  86. procedure TParseWebIDLApplication.WriteHelp(Const Msg : String);
  87. begin
  88. if Msg<>'' then
  89. Writeln('Error : ',Msg);
  90. writeln('Usage: ', ExeName, ' -h');
  91. ExitCode:=Ord(Msg<>'');
  92. end;
  93. var
  94. Application: TParseWebIDLApplication;
  95. begin
  96. Application:=TParseWebIDLApplication.Create(nil);
  97. Application.Title:='Parse WEB IDL Application';
  98. Application.Run;
  99. Application.Free;
  100. end.