* Resolve all identifiers

This commit is contained in:
Michaël Van Canneyt 2024-04-23 14:16:48 +02:00
parent 2e86cdb620
commit 0a4f5558be
5 changed files with 1555 additions and 702 deletions

View File

@ -183,6 +183,7 @@ type
Function AsString(Full : Boolean): UTF8String; override;
Function HasAttributes : Boolean;
Function HasSimpleAttribute(Const AName : UTF8String) : Boolean;
Function GetNamePath : String;
Property Name : UTF8String Read FName Write FName;
Property Data : TObject Read FData Write FData;
Property Parent : TIDLDefinition Read FParent Write FParent;
@ -462,6 +463,7 @@ type
TIDLTypeDefDefinition = Class(TIDLTypeDefinition)
private
FIsTypeDef: Boolean;
FNull: Boolean;
FTypeName: String;
Public
@ -470,6 +472,7 @@ type
Function AsString(Full: Boolean): UTF8String; override;
Property TypeName : String Read FTypeName Write FTypeName;
Property AllowNull : Boolean Read FNull Write FNull;
Property IsTypeDef : Boolean Read FIsTypeDef Write FIsTypeDef;
end;
TIDLTypeDefDefinitionClass = Class of TIDLTypeDefDefinition;
@ -542,6 +545,7 @@ type
FIsReadonly: Boolean;
procedure SetElementType(AValue: TIDLTypeDefDefinition);
Public
Function GetJSTypeName: String; override;
Function AsString(Full: Boolean): UTF8String; override;
Destructor Destroy; override;
property ElementType : TIDLTypeDefDefinition Read FElementType Write SetElementType;
@ -628,6 +632,11 @@ begin
FElementType.Parent:=Self
end;
function TIDLSetlikeDefinition.GetJSTypeName: String;
begin
Result:=Name;
end;
function TIDLSetlikeDefinition.AsString(Full: Boolean): UTF8String;
begin
Result:='setlike <'+ElementType.TypeName+'>';
@ -1542,5 +1551,44 @@ begin
Result:=HasAttributes and (FAttributes.IndexOf(aName)<>-1);
end;
function TIDLDefinition.GetNamePath: String;
Function GetName(Def : TIDLDefinition) : string;
var
Loc : String;
begin
if Def=Nil then
Result:='[Nil]'
else
begin
Result:=Def.Name;
if (Result='') then
begin
Result:=Def.ClassName;
if Self.Line<>0 then
Loc:=Format('at (%d,%d)',[line,Column])
else
Loc:='';
Result:=Format('<anonymous(%s)%s>',[Result,Loc]);
end;
end;
end;
var
P : TIDLDefinition;
begin
Result:=GetName(Self);
P:=Self.Parent;
While Assigned(P) do
begin
Result:=GetName(P)+'.'+Result;
P:=P.Parent;
end;
end;
end.

View File

@ -20,6 +20,8 @@ unit webidlparser;
{$IF FPC_FULLVERSION>=30301}
{$WARN 6060 off : }
{$ENDIF}
{.$DEFINE VerboseWebIDLParser}
interface
{$IFDEF FPC_DOTTEDUNITS}
@ -1379,6 +1381,8 @@ begin
ExpectToken(tkLess);
Result.ElementType:=ParseType(Result);
Result.ElementType.Parent:=Result;
if (Result.ElementType.Name='') then
Result.ElementType.Name:=Result.ElementType.TypeName;
CheckCurrentToken(tkLarger);
ok:=true;
finally
@ -1558,6 +1562,7 @@ begin
try
CheckCurrentToken(tkIdentifier);
Result.Name:=CurrentTokenString;
Result.IsTypeDef:=True;
ok:=true;
finally
if not ok then
@ -1923,9 +1928,13 @@ var
function GetTopologicalLevel(Top: TTopologicalIntf): integer;
var
ParentTop: TTopologicalIntf;
{$IFDEF VerboseWebIDLParser}
IntfDef: TIDLInterfaceDefinition;
{$ENDIF}
begin
{$IFDEF VerboseWebIDLParser}
IntfDef:=Top.Intf;
{$ENDIF}
if Top.Level<0 then
begin
if Top.Parent=nil then
@ -1935,7 +1944,9 @@ var
ParentTop:=FindIntf(Top.Parent);
if ParentTop=nil then
begin
writeln('Warning: [20220725182101] [TWebIDLContext.GetInterfacesTopologically] interface "'+IntfDef.Name+'" at '+GetDefPos(IntfDef)+', parent "'+Top.Parent.Name+'" at '+GetDefPos(Top.Parent)+' not in definition list');
{$IFDEF VerboseWebIDLParser}
Log('Warning: [20220725182101] [TWebIDLContext.GetInterfacesTopologically] interface "'+IntfDef.Name+'" at '+GetDefPos(IntfDef)+', parent "'+Top.Parent.Name+'" at '+GetDefPos(Top.Parent)+' not in definition list');
{$ENDIF}
Top.Level:=0;
end
else

File diff suppressed because it is too large Load Diff

View File

@ -52,8 +52,7 @@ type
Function BaseUnits: String; override;
// Auxiliary routines
procedure GetOptions(L: TStrings; Full: boolean); override;
function GetPascalTypeName(const aTypeName: String; ForTypeDef: Boolean=False
): String; override;
function GetPascalTypeName(const aTypeName: String; ForTypeDef: Boolean=False ): String; override;
function GetInterfaceDefHead(Intf: TIDLInterfaceDefinition): String;
override;
// Code generation routines. Return the number of actually written defs.
@ -115,8 +114,7 @@ begin
L.Add('Extended Options: '+Pas2jsConversionOptionsToStr(Pas2jsOptions));
end;
function TWebIDLToPas2js.GetPascalTypeName(const aTypeName: String;
ForTypeDef: Boolean): String;
function TWebIDLToPas2js.GetPascalTypeName(const aTypeName: String; ForTypeDef: Boolean): String;
Function UsePascalType(Const aPascalType: string): String;
@ -157,7 +155,7 @@ var
begin
Result:='class external name '+MakePascalString(Intf.Name,True);
if Assigned(Intf.ParentInterface) then
aParentName:=GetName(Intf.ParentInterface)
aParentName:=GetPasName(Intf.ParentInterface)
else
aParentName:=GetPascalTypeName(Intf.ParentName);
if aParentName<>'' then
@ -179,10 +177,10 @@ begin
RT:='';
if not (foConstructor in aDef.Options) then
begin
FN:=GetName(aDef);
FN:=GetPasName(aDef);
if FN<>aDef.Name then
Suff:=Format('; external name ''%s''',[aDef.Name]);
RT:=GetTypeName(aDef.ReturnType,False);
RT:=GetJSTypeName(aDef.ReturnType);
if (RT='void') then
RT:='';
end
@ -258,7 +256,7 @@ begin
if p2jcoExternalConst in Pas2jsOptions then
begin
S:=ConstTypes[aConst.ConstType];
Addln('%s: %s;',[GetName(aConst),S])
Addln('%s: %s;',[GetPasName(aConst),S])
end
else
Result:=inherited WriteConst(aConst);
@ -270,13 +268,13 @@ Var
begin
Result:=True;
N:=GetName(aAttr);
N:=GetPasName(aAttr);
if aAttr.AttributeType=nil then
begin
AddLn('skipping field without type: "'+N+'"');
exit;
end;
TN:=GetTypeName(aAttr.AttributeType);
TN:=GetJSTypeName(aAttr.AttributeType);
if TN='record' then
TN:='TJSObject';
if SameText(N,TN) then
@ -290,7 +288,7 @@ end;
function TWebIDLToPas2js.WritePrivateReadOnlyField(
aAttr: TIDLAttributeDefinition): Boolean;
begin
AddLn('%s%s: %s; external name ''%s''; ',[FieldPrefix,GetName(aAttr),GetTypeName(aAttr.AttributeType),aAttr.Name]);
AddLn('%s%s: %s; external name ''%s''; ',[FieldPrefix,GetPasName(aAttr),GetPascalTypeName(aAttr.AttributeType),aAttr.Name]);
Result:=true;
end;
@ -303,9 +301,9 @@ Var
begin
Result:=True;
if aParent=nil then ;
N:=GetName(aAttr);
N:=GetPasName(aAttr);
PN:=N;
TN:=GetTypeName(aAttr.AttributeType);
TN:=GetPascalTypeName(aAttr.AttributeType);
if SameText(PN,TN) then
PN:='_'+PN;
AddLn('Property %s: %s Read %s%s; ',[PN,TN,FieldPrefix,N]);

File diff suppressed because it is too large Load Diff