pas2js: option -im, -M<modeswitch>

git-svn-id: trunk@44121 -
This commit is contained in:
Mattias Gaertner 2020-02-06 16:05:09 +00:00
parent 8de6066b95
commit f053fa926f
4 changed files with 98 additions and 28 deletions

View File

@ -1167,6 +1167,7 @@ const
const
// all mode switches supported by FPC
msAllModeSwitches = [low(TModeSwitch)..High(TModeSwitch)];
msAllModes = [msFpc..msGPC];
DelphiModeSwitches = [msDelphi,msClass,msObjpas,msResult,msStringPchar,
msPointer2Procedure,msAutoDeref,msTPProcVar,msInitFinal,msDefaultAnsistring,

View File

@ -53,7 +53,7 @@ const
const
nOptionIsEnabled = 101; sOptionIsEnabled = 'Option "%s" is %s';
nSyntaxModeIs = 102; sSyntaxModeIs = 'Syntax mode is %s';
// was: nMacroDefined = 103
nModeswitchXisY = 103; sModeswitchXisY = 'Modeswitch %s is %s';
// 104 in unit Pas2JSFS
// 105 in unit Pas2JSFS
nNameValue = 106; sNameValue = '%s: %s';
@ -491,7 +491,7 @@ type
FMainJSFileIsResolved: Boolean;
FMainJSFileResolved: String;
FMainSrcFile: String;
FMode: TP2jsMode;
FModeSwitches: TModeSwitches;
FNamespaces: TStringList;
FNamespacesFromCmdLine: integer;
FOptions: TP2jsCompilerOptions;
@ -543,6 +543,7 @@ type
procedure SetCompilerExe(AValue: string);
procedure SetFS(AValue: TPas2jsFS);
procedure SetMode(AValue: TP2jsMode);
procedure SetModeSwitches(const AValue: TModeSwitches);
procedure SetOptions(AValue: TP2jsCompilerOptions);
procedure SetShowDebug(AValue: boolean);
procedure SetShowFullPaths(AValue: boolean);
@ -602,6 +603,7 @@ type
procedure HandleOptionPCUFormat(aValue: String); virtual;
function HandleOptionPaths(C: Char; aValue: String; FromCmdLine: Boolean): Boolean; virtual;
function HandleOptionJ(C: Char; aValue: String; Quick,FromCmdLine: Boolean): Boolean; virtual;
function HandleOptionM(aValue: String; Quick: Boolean): Boolean; virtual;
procedure HandleOptionConfigFile(aPos: Integer; const aFileName: string); virtual;
procedure HandleOptionInfo(aValue: string);
// DoWriteJSFile: return false to use the default write function.
@ -683,7 +685,7 @@ type
property InterfaceType: TPasClassInterfaceType read FInterfaceType write FInterfaceType;
property Log: TPas2jsLogger read FLog;
property MainFile: TPas2jsCompilerFile read FMainFile;
property Mode: TP2jsMode read FMode write SetMode;
property ModeSwitches: TModeSwitches read FModeSwitches write SetModeSwitches;
property Options: TP2jsCompilerOptions read FOptions write SetOptions;
property ConverterGlobals: TPasToJSConverterGlobals read FConverterGlobals write SetConverterGlobals;
property ParamMacros: TPas2jsMacroEngine read FParamMacros;
@ -1002,7 +1004,7 @@ end;
function TPas2jsCompilerFile.GetInitialModeSwitches: TModeSwitches;
begin
Result:=p2jsMode_SwitchSets[Compiler.Mode];
Result:=Compiler.ModeSwitches;
end;
function TPas2jsCompilerFile.GetInitialBoolSwitches: TBoolSwitches;
@ -1089,8 +1091,6 @@ begin
Scanner.CurrentValueSwitch[vsInterfaces]:=InterfaceTypeNames[Compiler.InterfaceType];
if coAllowCAssignments in Compiler.Options then
Scanner.Options:=Scanner.Options+[po_cassignments];
if Compiler.Mode=p2jmDelphi then
Scanner.Options:=Scanner.Options+[po_delphi];
// Note: some Scanner.Options are set by TPasResolver
for i:=0 to Compiler.Defines.Count-1 do
begin
@ -1953,7 +1953,7 @@ begin
// check modeswitches
ms:=StrToModeSwitch(aName);
if (ms<>msNone) and (ms in p2jsMode_SwitchSets[Compiler.Mode]) then
if (ms<>msNone) and (ms in Compiler.ModeSwitches) then
begin
Value:=CondDirectiveBool[true];
exit(true);
@ -3072,14 +3072,19 @@ end;
procedure TPas2jsCompiler.SetMode(AValue: TP2jsMode);
begin
if FMode=AValue then Exit;
FMode:=AValue;
case FMode of
SetModeSwitches(p2jsMode_SwitchSets[AValue]);
case AValue of
p2jmObjFPC: Options:=Options-[coAllowCAssignments];
p2jmDelphi: Options:=Options-[coAllowCAssignments];
end;
end;
procedure TPas2jsCompiler.SetModeSwitches(const AValue: TModeSwitches);
begin
if FModeSwitches=AValue then Exit;
FModeSwitches:=AValue;
end;
procedure TPas2jsCompiler.SetOptions(AValue: TP2jsCompilerOptions);
begin
if FOptions=AValue then Exit;
@ -3241,6 +3246,7 @@ begin
LastMsgNumber:=-1;
r(mtInfo,nOptionIsEnabled,sOptionIsEnabled);
r(mtInfo,nSyntaxModeIs,sSyntaxModeIs);
r(mtInfo,nModeswitchXisY,sModeswitchXisY);
LastMsgNumber:=-1; // was nMacroDefined 103
r(mtInfo,nUsingPath,sUsingPath);
r(mtNote,nFolderNotFound,sFolderNotFound);
@ -3583,6 +3589,49 @@ begin
end;
end;
function TPas2jsCompiler.HandleOptionM(aValue: String; Quick: Boolean): Boolean;
var
Negated: Boolean;
ms: TModeSwitch;
begin
Result:=True;
if aValue='' then
ParamFatal('invalid syntax mode (-M<x>) "'+aValue+'"');
if Quick then exit;
case lowerCase(aValue) of
'delphi': SetMode(p2jmDelphi);
'objfpc': SetMode(p2jmObjFPC);
else
if aValue[length(aValue)]='-' then
begin
aValue:=LeftStr(aValue,length(aValue)-1);
Negated:=true;
end else
Negated:=false;
for ms in TModeSwitch do
if (ms in msAllPas2jsModeSwitches)
and SameText(SModeSwitchNames[ms],aValue) then
begin
if (ms in ModeSwitches)<>Negated then
begin
// already set
exit;
end else if ms in msAllPas2jsModeSwitchesReadOnly then
ParamFatal('modeswitch is read only -M"'+aValue+'"')
else begin
// switch
if Negated then
ModeSwitches:=ModeSwitches-[ms]
else
ModeSwitches:=ModeSwitches+[ms];
exit;
end;
end;
ParamFatal('invalid syntax mode (-M) "'+aValue+'"');
end;
end;
procedure TPas2jsCompiler.HandleOptionConfigFile(aPos: Integer; const aFileName: string);
Var
@ -3617,6 +3666,7 @@ Var
pl: TPasToJsPlatform;
s: string;
pbi: TPas2JSBuiltInName;
ms: TModeSwitch;
begin
// write information and halt
InfoMsg:='';
@ -3672,6 +3722,12 @@ begin
// write list of supported JS processors
for pr in TPasToJsProcessor do
Log.LogPlain(PasToJsProcessorNames[pr]);
'm':
begin
// write list of supported modeswitches
for ms in (msAllPas2jsModeSwitches-msAllModes) do
Log.LogPlain(SModeSwitchNames[ms]);
end;
'o':
begin
// write list of optimizations
@ -3813,14 +3869,8 @@ begin
UnknownParam;
end;
'M': // syntax mode
begin
case lowerCase(aValue) of
'delphi': Mode:=p2jmDelphi;
'objfpc': Mode:=p2jmObjFPC;
else
ParamFatal('invalid syntax mode (-M) "'+aValue+'"');
end;
end;
if not HandleOptionM(aValue,Quick) then
UnknownParam;
'N':
begin
if aValue='' then
@ -3997,10 +4047,10 @@ begin
ReadSingleLetterOptions(Param,p,'2acdmj',Enabled,Disabled);
for i:=1 to length(Enabled) do begin
case Enabled[i] of
'2': Mode:=p2jmObjFPC;
'2': SetMode(p2jmObjFPC);
'a': Options:=Options+[coAssertions];
'c': Options:=Options+[coAllowCAssignments];
'd': Mode:=p2jmDelphi;
'd': SetMode(p2jmDelphi);
'm': Options:=Options+[coAllowMacros];
'j': Options:=Options+[coWriteableConst];
end;
@ -4374,7 +4424,7 @@ begin
FMainSrcFile:='';
FOptions:=DefaultP2jsCompilerOptions;
FRTLVersionCheck:=DefaultP2jsRTLVersionCheck;
FMode:=p2jmObjFPC;
FModeSwitches:=p2jsMode_SwitchSets[p2jmObjFPC];
FConverterGlobals.Reset;
FConverterGlobals.RTLVersion:=(VersionMajor*100+VersionMinor)*100+VersionRelease;
FConverterGlobals.TargetPlatform:=PlatformBrowser;
@ -4600,6 +4650,7 @@ begin
w(' -iV : Write short compiler version');
w(' -iW : Write full compiler version');
w(' -ic : Write list of supported JS processors usable by -P<x>');
w(' -im : Write list of supported modeswitches usable by -M<x>');
w(' -io : Write list of supported optimizations usable by -Oo<x>');
w(' -it : Write list of supported targets usable by -T<x>');
w(' -iJ : Write list of supported JavaScript identifiers -JoRTL-<x>');
@ -4655,8 +4706,12 @@ begin
w(' -Ju<x>: Add <x> to foreign unit paths. Foreign units are not compiled.');
WritePrecompiledFormats;
w(' -l : Write logo');
w(' -MDelphi: Delphi 7 compatibility mode');
w(' -MObjFPC: FPC''s Object Pascal compatibility mode (default)');
w(' -M<x> : Set language mode or enable/disable a modeswitch');
w(' -MDelphi: Delphi 7 compatibility mode');
w(' -MObjFPC: FPC''s Object Pascal compatibility mode (default)');
w(' -M<x> : enable or disable modeswitch, see option -im');
w(' Each mode (as listed above) enables its default set of modeswitches.');
w(' Other modeswitches are disabled and need to be enabled one by another.');
w(' -NS<x> : obsolete: add <x> to namespaces. Same as -FN<x>');
w(' -n : Do not read the default config files');
w(' -o<x> : Change main JavaScript file to <x>, "." means stdout');
@ -4747,14 +4802,25 @@ procedure TPas2jsCompiler.WriteOptions;
var
co: TP2jsCompilerOption;
fco: TP2jsFSOption;
ms: TModeSwitch;
begin
// message encoding
WriteEncoding;
// target platform
Log.LogMsgIgnoreFilter(nTargetPlatformIs,[PasToJsPlatformNames[TargetPlatform]]);
Log.LogMsgIgnoreFilter(nTargetProcessorIs,[PasToJsProcessorNames[TargetProcessor]]);
// default syntax mode
Log.LogMsgIgnoreFilter(nSyntaxModeIs,[p2jscModeNames[Mode]]);
// syntax mode
for ms in msAllPas2jsModeSwitches do
case ms of
msObjfpc:
if ms in ModeSwitches then
Log.LogMsgIgnoreFilter(nSyntaxModeIs,[p2jscModeNames[p2jmObjFPC]]);
msDelphi:
if ms in ModeSwitches then
Log.LogMsgIgnoreFilter(nSyntaxModeIs,[p2jscModeNames[p2jmDelphi]]);
else
Log.LogMsgIgnoreFilter(nModeswitchXisY,[SModeSwitchNames[ms],BoolToStr(ms in ModeSwitches,'on','off')]);
end;
Log.LogMsgIgnoreFilter(nClassInterfaceStyleIs,[InterfaceTypeNames[InterfaceType]]);
// boolean options
for co in TP2jsCompilerOption do

View File

@ -133,6 +133,7 @@ Put + after a boolean switch option to enable it, - to disable it
-iV : Write short compiler version
-iW : Write full compiler version
-ic : Write list of supported JS processors usable by -P&lt;x&gt;
-im : Write list of supported modeswitches usable by -M&lt;x&gt;
-io : Write list of supported optimizations usable by -Oo&lt;x&gt;
-it : Write list of supported targets usable by -T&lt;x&gt;
-iJ : Write list of supported JavaScript identifiers -JoRTL-&lt;x&gt;
@ -179,8 +180,11 @@ Put + after a boolean switch option to enable it, - to disable it
postprocessors in succession.
-Ju&lt;x&gt; : Add &lt;x&gt; to foreign unit paths. Foreign units are not compiled.
-l : Write logo
-MDelphi: Delphi 7 compatibility mode
-MObjFPC: FPC's Object Pascal compatibility mode (default)
-M&lt;x&gt; : Set language mode or enable/disable a modeswitch
-MDelphi: Delphi 7 compatibility mode
-MObjFPC: FPC's Object Pascal compatibility mode (default)
Each mode (as listed above) enables its default set of modeswitches.
Other modeswitches are disabled and need to be enabled one by another.
-NS&lt;x&gt; : obsolete: add &lt;x&gt; to namespaces. Same as -FN&lt;x&gt;
-n : Do not read the default config files
-o&lt;x&gt; : Change main JavaScript file to &lt;x&gt;, "." means stdout

View File

@ -22,7 +22,6 @@ begin
P.Description := 'Convert pascal sources to javascript.';
P.Email := 'michael@freepascal.org';
P.NeedLibC:= false;
P.ShortName:='p2js';
P.Directory:=ADirectory;
P.Version:='3.3.1';