From bfc5d63fdeb783908833cc51e15bee992138c918 Mon Sep 17 00:00:00 2001 From: michael Date: Sat, 23 Jun 2018 14:02:55 +0000 Subject: [PATCH] * Add webidl2pas git-svn-id: trunk@39293 - --- .gitattributes | 2 + utils/pas2js/fpmake.pp | 2 + utils/pas2js/webidl2pas.lpi | 64 ++++++++++++ utils/pas2js/webidl2pas.pp | 190 ++++++++++++++++++++++++++++++++++++ 4 files changed, 258 insertions(+) create mode 100644 utils/pas2js/webidl2pas.lpi create mode 100644 utils/pas2js/webidl2pas.pp diff --git a/.gitattributes b/.gitattributes index 24123a2a13..6f96ff9557 100644 --- a/.gitattributes +++ b/.gitattributes @@ -17213,6 +17213,8 @@ utils/pas2js/samples/hello.pas svneol=native#text/plain utils/pas2js/samples/ifdemo.pp svneol=native#text/plain utils/pas2js/samples/repeatdemo.pp svneol=native#text/plain utils/pas2js/samples/whiledemo.pp svneol=native#text/plain +utils/pas2js/webidl2pas.lpi svneol=native#text/plain +utils/pas2js/webidl2pas.pp svneol=native#text/plain utils/pas2ut/Makefile svneol=native#text/plain utils/pas2ut/Makefile.fpc svneol=native#text/plain utils/pas2ut/Makefile.fpc.fpcmake svneol=native#text/plain diff --git a/utils/pas2js/fpmake.pp b/utils/pas2js/fpmake.pp index 422ab71130..b98b09a6c5 100644 --- a/utils/pas2js/fpmake.pp +++ b/utils/pas2js/fpmake.pp @@ -31,11 +31,13 @@ begin P.Dependencies.Add('fcl-passrc'); P.Dependencies.Add('pastojs'); P.Dependencies.Add('fcl-web'); + P.Dependencies.Add('webidl'); PT:=P.Targets.AddProgram('pas2js.pp'); PT:=P.Targets.AddLibrary('pas2jslib.pp'); PT:=P.Targets.AddUnit('httpcompiler.pp'); PT:=P.Targets.AddProgram('compileserver.pp'); PT.Dependencies.AddUnit('httpcompiler'); + PT:=P.Targets.AddProgram('webidl2pas.pp'); end; end; diff --git a/utils/pas2js/webidl2pas.lpi b/utils/pas2js/webidl2pas.lpi new file mode 100644 index 0000000000..596fa6375c --- /dev/null +++ b/utils/pas2js/webidl2pas.lpi @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <BuildModes Count="1"> + <Item1 Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + <Modes Count="0"/> + </RunParams> + <Units Count="1"> + <Unit0> + <Filename Value="webidl2pas.pp"/> + <IsPartOfProject Value="True"/> + </Unit0> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <Target> + <Filename Value="webidl2pas"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + <Linking> + <Debugging> + <UseHeaptrc Value="True"/> + </Debugging> + </Linking> + </CompilerOptions> + <Debugging> + <Exceptions Count="3"> + <Item1> + <Name Value="EAbort"/> + </Item1> + <Item2> + <Name Value="ECodetoolError"/> + </Item2> + <Item3> + <Name Value="EFOpenError"/> + </Item3> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/utils/pas2js/webidl2pas.pp b/utils/pas2js/webidl2pas.pp new file mode 100644 index 0000000000..24f0b64f02 --- /dev/null +++ b/utils/pas2js/webidl2pas.pp @@ -0,0 +1,190 @@ +program webidl2pas; + +{$mode objfpc}{$H+} + +uses + {$IFDEF UNIX}{$IFDEF UseCThreads} + cthreads, + {$ENDIF}{$ENDIF} + Classes, SysUtils, CustApp, webidltopas, pascodegen + { you can add units after this }; + +type + + { TWebIDLToPasApplication } + + TWebIDLToPasApplication = class(TCustomApplication) + private + FWebIDLToPas: TWebIDLToPas; + function Checkoption(Var O: TCOnversionOPtions; C: TCOnversionOPtion; + const AShort: Char; const aLong: String): Boolean; + procedure DoConvertLog(Sender: TObject; LogType: TCodegenLogType; const Msg: String); + function GetInputFileName: String; + function GetOutputFileName: String; + function GetUnitName: String; + procedure SetinputFileName(AValue: String); + procedure SetOutputFileName(AValue: String); + procedure SetunitName(AValue: String); + protected + procedure DoRun; override; + Protected + Property WebIDLToPas : TWebIDLToPas Read FWebIDLToPas; + public + constructor Create(TheOwner: TComponent); override; + destructor Destroy; override; + procedure WriteHelp(Const Msg : string); virtual; + Property UnitName : String Read GetUnitName Write SetunitName; + property InputFileName : String Read GetInputFileName Write SetinputFileName; + property OutputFileName : String Read GetOutputFileName Write SetOutputFileName; + end; + +{ TWebIDLToPasApplication } + +function TWebIDLToPasApplication.GetInputFileName: String; +begin + Result:=FWebIDLToPas.InputFileName; +end; + +procedure TWebIDLToPasApplication.DoConvertLog(Sender: TObject; + LogType: TCodegenLogType; const Msg: String); +begin + Writeln(Msg); +end; + +function TWebIDLToPasApplication.GetOutputFileName: String; +begin + Result:=FWebIDLToPas.OutputFileName +end; + +function TWebIDLToPasApplication.GetUnitName: String; +begin + Result:=FWebIDLToPas.OutputUnitName; +end; + +procedure TWebIDLToPasApplication.SetinputFileName(AValue: String); +begin + FWebIDLToPas.InputFileName:=aValue; +end; + +procedure TWebIDLToPasApplication.SetOutputFileName(AValue: String); +begin + FWebIDLToPas.OutputFileName:=aValue; +end; + +procedure TWebIDLToPasApplication.SetunitName(AValue: String); +begin + FWebIDLToPas.OutputUnitName:=aValue; +end; + +Function TWebIDLToPasApplication.Checkoption(Var O : TCOnversionOPtions;C : TCOnversionOPtion; Const AShort : Char; Const aLong : String) : Boolean; + +begin + Result:=HasOption(aShort,ALong); + if Result then + Include(O,C); +end; + +procedure TWebIDLToPasApplication.DoRun; + +var + A,ErrorMsg: String; + O : TConversionOptions; + +begin + + Terminate; + // quick check parameters + ErrorMsg:=CheckOptions('hi:o:u:m:n:vx:t:ced::p', ['help','input:','output:','unitname:','include:','implementation:','verbose','extra:','typealiases:','constexternal','expandunionargs','dicttoclass::','optionsinheader']); + if (ErrorMsg<>'') or HasOption('h','help') then + begin + WriteHelp(ErrorMsg); + Exit; + end; + O:=[]; + Checkoption(O,coExternalConst,'c','constexternal'); + Checkoption(O,coExpandUnionTypeArgs,'e','expandunionargs'); + CheckOption(O,coaddOptionsToheader,'p','optionsinheader'); + if Checkoption(O,coDictionaryAsClass,'d','dicttoclass') then + FWebIDLToPas.DictionaryClassParent:=GetOptionValue('d','dicttoclass'); + FWebIDLToPas.Options:=O; + InputFileName:=GetOptionValue('i','input'); + OutputFileName:=GetOptionValue('o','output'); + UnitName:=GetOptionValue('u','unitname'); + FWebIDLToPas.Verbose:=HasOption('v','verbose'); + if hasoption('n','include') then + FWebIDLToPas.IncludeInterfaceCode.LoadFromFile(GetOptionValue('n','include')); + if hasoption('m','implementation') then + FWebIDLToPas.IncludeImplementationCode.LoadFromFile(GetOptionValue('m','implementation')); + FWebIDLToPas.ExtraUnits:=GetOPtionValue('x','extra'); + A:=GetOptionValue('t','typealiases'); + if (Copy(A,1,1)='@') then + begin + Delete(A,1,1); + FWebIDLToPas.TypeAliases.LoadFromFile(A); + end + else + FWebIDLToPas.TypeAliases.CommaText:=A; + if UnitName='' then + UnitName:=ChangeFileExt(ExtractFileName(InputFileName),''); + if OutputFileName='' then + begin + if (UnitName<>'') then + OutputFileName:=ExtractFilePath(InputFileName)+UnitName+'.pas'; + end; + FWebIDLToPas.Execute; + // stop program loop + Terminate; +end; + +constructor TWebIDLToPasApplication.Create(TheOwner: TComponent); +begin + inherited Create(TheOwner); + StopOnException:=True; + FWebIDLToPas:=TWebIDLToPas.Create(Self); + FWebIDLToPas.OnLog:=@DoConvertLog; + FWebIDLToPas.ClassPrefix:='TJS'; + FWebIDLToPas.ClassSuffix:=''; + FWebIDLToPas.KeywordSuffix:='_'; + FWebIDLToPas.KeywordPrefix:=''; +end; + +destructor TWebIDLToPasApplication.Destroy; +begin + FreeAndNil(FWebIDLToPas); + inherited Destroy; +end; + +procedure TWebIDLToPasApplication.WriteHelp(const Msg: string); +begin + { add your help code here } + if (Msg<>'') then + Writeln(StdErr,'Error : ',Msg); + writeln(StdErr,'Usage: ', ExeName, ' [options]'); + Writeln(StdErr,'Where option is one or more of'); + Writeln(StdErr,'-h --help this help text'); + Writeln(StdErr,'-c --constexternal Write consts as external const (no value)'); + Writeln(StdErr,'-e --expandunionargs Add overloads for all Union typed function arguments'); + Writeln(StdErr,'-d --dicttoclass[=Parent] Write dictionaries as classes'); + Writeln(StdErr,'-i --input=FileName input webidl file'); + Writeln(StdErr,'-m --implementation=Filename include file as implementation'); + Writeln(StdErr,'-n --include=Filename include file at end of interface'); + Writeln(StdErr,'-o --output=FileName output file. Defaults to unit name with .pas extension appended.'); + Writeln(StdErr,'-p --optionsinheader add options to header of generated file'); + + Writeln(StdErr,'-t --typealiases=alias A comma separated list of type aliases in Alias=Name form'); + Writeln(StdErr,' use @filename to load the aliases from file.'); + Writeln(StdErr,'-u --unitname=Name name for unit. Defaults to input file without extension.'); + Writeln(StdErr,'-v --verbose Output some diagnostic information'); + Writeln(StdErr,'-x --extra=units Extra units to put in uses clause (comma separated list)'); + ExitCode:=Ord(Msg<>''); +end; + +var + Application: TWebIDLToPasApplication; +begin + Application:=TWebIDLToPasApplication.Create(nil); + Application.Title:='WebIDL To Pascal converter Application'; + Application.Run; + Application.Free; +end. +