Browse Source

* Slightly better identifier position

Michaël Van Canneyt 3 years ago
parent
commit
36eea0c819
1 changed files with 25 additions and 7 deletions
  1. 25 7
      packages/fcl-passrc/src/pparser.pp

+ 25 - 7
packages/fcl-passrc/src/pparser.pp

@@ -253,6 +253,7 @@ type
   private
   private
     FCurModule: TPasModule;
     FCurModule: TPasModule;
     FFileResolver: TBaseFileResolver;
     FFileResolver: TBaseFileResolver;
+    FIdentifierPos: TPasSourcePos;
     FImplicitUses: TStrings;
     FImplicitUses: TStrings;
     FLastMsg: string;
     FLastMsg: string;
     FLastMsgArgs: TMessageArgs;
     FLastMsgArgs: TMessageArgs;
@@ -390,6 +391,7 @@ type
     procedure ExpectTokens(tk:  TTokens);
     procedure ExpectTokens(tk:  TTokens);
     function GetPrevToken: TToken;
     function GetPrevToken: TToken;
     function ExpectIdentifier: String;
     function ExpectIdentifier: String;
+    Procedure SaveIdentifierPosition;
     Function CurTokenIsIdentifier(Const S : String) : Boolean;
     Function CurTokenIsIdentifier(Const S : String) : Boolean;
     // Expression parsing
     // Expression parsing
     function isEndOfExp(AllowEqual : Boolean = False; CheckHints : Boolean = True): Boolean;
     function isEndOfExp(AllowEqual : Boolean = False; CheckHints : Boolean = True): Boolean;
@@ -398,7 +400,8 @@ type
     // Type declarations
     // Type declarations
     function ResolveTypeReference(Name: string; Parent: TPasElement; ParamCnt: integer = 0): TPasType;
     function ResolveTypeReference(Name: string; Parent: TPasElement; ParamCnt: integer = 0): TPasType;
     function ParseVarType(Parent : TPasElement = Nil): TPasType;
     function ParseVarType(Parent : TPasElement = Nil): TPasType;
-    function ParseTypeDecl(Parent: TPasElement): TPasType;
+    function ParseTypeDecl(Parent: TPasElement): TPasType; overload;
+    function ParseTypeDecl(Parent: TPasElement; NamePos : TPasSourcePos): TPasType; overload;
     function ParseGenericTypeDecl(Parent: TPasElement; AddToParent: boolean): TPasGenericType;
     function ParseGenericTypeDecl(Parent: TPasElement; AddToParent: boolean): TPasGenericType;
     function ParseType(Parent: TPasElement; const NamePos: TPasSourcePos; const TypeName: String = ''; Full: Boolean = false): TPasType;
     function ParseType(Parent: TPasElement; const NamePos: TPasSourcePos; const TypeName: String = ''; Full: Boolean = false): TPasType;
     function ParseReferenceToProcedureType(Parent: TPasElement; Const NamePos: TPasSourcePos; Const TypeName: String): TPasProcedureType;
     function ParseReferenceToProcedureType(Parent: TPasElement; Const NamePos: TPasSourcePos; Const TypeName: String): TPasProcedureType;
@@ -473,6 +476,7 @@ type
     property LastMsgType: TMessageType read FLastMsgType write FLastMsgType;
     property LastMsgType: TMessageType read FLastMsgType write FLastMsgType;
     property LastMsgPattern: string read FLastMsgPattern write FLastMsgPattern;
     property LastMsgPattern: string read FLastMsgPattern write FLastMsgPattern;
     property LastMsgArgs: TMessageArgs read FLastMsgArgs write FLastMsgArgs;
     property LastMsgArgs: TMessageArgs read FLastMsgArgs write FLastMsgArgs;
+    Property IdentifierPosition : TPasSourcePos Read FIdentifierPos;
   end;
   end;
 
 
 Type
 Type
@@ -1319,6 +1323,11 @@ begin
   Result := CurTokenString;
   Result := CurTokenString;
 end;
 end;
 
 
+procedure TPasParser.SaveIdentifierPosition;
+begin
+  FIdentifierPos:=FScanner.CurSourcePos;
+end;
+
 function TPasParser.CurTokenIsIdentifier(const S: String): Boolean;
 function TPasParser.CurTokenIsIdentifier(const S: String): Boolean;
 begin
 begin
   Result:=(Curtoken=tkIdentifier) and (CompareText(S,CurtokenText)=0);
   Result:=(Curtoken=tkIdentifier) and (CompareText(S,CurtokenText)=0);
@@ -3690,6 +3699,7 @@ begin
       begin
       begin
       Scanner.UnSetTokenOption(toOperatorToken);
       Scanner.UnSetTokenOption(toOperatorToken);
       SaveComments;
       SaveComments;
+      SaveIdentifierPosition;
       case CurBlock of
       case CurBlock of
         declConst:
         declConst:
           begin
           begin
@@ -3707,7 +3717,7 @@ begin
           end;
           end;
         declType:
         declType:
           begin
           begin
-          TypeEl := ParseTypeDecl(Declarations);
+          TypeEl := ParseTypeDecl(Declarations,IdentifierPosition);
           // Scanner.SetForceCaret(OldForceCaret); // It may have been switched off
           // Scanner.SetForceCaret(OldForceCaret); // It may have been switched off
           if Assigned(TypeEl) then        // !!!
           if Assigned(TypeEl) then        // !!!
             begin
             begin
@@ -4060,7 +4070,7 @@ var
   OldForceCaret,ok: Boolean;
   OldForceCaret,ok: Boolean;
 begin
 begin
   SaveComments;
   SaveComments;
-  Result := TPasConst(CreateElement(TPasConst, CurTokenString, Parent));
+  Result := TPasConst(CreateElement(TPasConst, CurTokenString, Parent, IdentifierPosition));
   if Parent is TPasMembersType then
   if Parent is TPasMembersType then
     Include(Result.VarModifiers,vmClass);
     Include(Result.VarModifiers,vmClass);
   ok:=false;
   ok:=false;
@@ -4154,7 +4164,7 @@ var
   ok: Boolean;
   ok: Boolean;
 begin
 begin
   SaveComments;
   SaveComments;
-  Result := TPasResString(CreateElement(TPasResString, CurTokenString, Parent));
+  Result := TPasResString(CreateElement(TPasResString, CurTokenString, Parent,IdentifierPosition));
   ok:=false;
   ok:=false;
   try
   try
     ExpectToken(tkEqual);
     ExpectToken(tkEqual);
@@ -4441,13 +4451,20 @@ begin
 end;
 end;
 
 
 function TPasParser.ParseTypeDecl(Parent: TPasElement): TPasType;
 function TPasParser.ParseTypeDecl(Parent: TPasElement): TPasType;
+
+begin
+  Result:=ParseTypeDecl(Parent,CurSourcePos);
+end;
+
+function TPasParser.ParseTypeDecl(Parent: TPasElement; NamePos : TPasSourcePos): TPasType;
+
 var
 var
   TypeName: String;
   TypeName: String;
-  NamePos: TPasSourcePos;
   OldForceCaret , IsDelphiGenericType: Boolean;
   OldForceCaret , IsDelphiGenericType: Boolean;
+
 begin
 begin
-  OldForceCaret:=Scanner.SetForceCaret(True);
   try
   try
+    OldForceCaret:=Scanner.SetForceCaret(True);
     IsDelphiGenericType:=false;
     IsDelphiGenericType:=false;
     if (msDelphi in CurrentModeswitches) then
     if (msDelphi in CurrentModeswitches) then
       begin
       begin
@@ -4460,7 +4477,6 @@ begin
     else
     else
       begin
       begin
       TypeName := CurTokenString;
       TypeName := CurTokenString;
-      NamePos:=CurSourcePos;
       ExpectToken(tkEqual);
       ExpectToken(tkEqual);
       Result:=ParseType(Parent,NamePos,TypeName,True);
       Result:=ParseType(Parent,NamePos,TypeName,True);
       end;
       end;
@@ -7069,6 +7085,7 @@ begin
         if CheckSection then
         if CheckSection then
           continue;
           continue;
         ExpectToken(tkIdentifier);
         ExpectToken(tkIdentifier);
+        SaveIdentifierPosition;
         ParseMembersLocalConsts(ARec,v);
         ParseMembersLocalConsts(ARec,v);
         end;
         end;
       tkVar:
       tkVar:
@@ -7078,6 +7095,7 @@ begin
         if CheckSection then
         if CheckSection then
           continue;
           continue;
         ExpectToken(tkIdentifier);
         ExpectToken(tkIdentifier);
+        SaveIdentifierPosition;
         OldCount:=ARec.Members.Count;
         OldCount:=ARec.Members.Count;
         ParseInlineVarDecl(ARec, ARec.Members, v, AEndToken=tkBraceClose);
         ParseInlineVarDecl(ARec, ARec.Members, v, AEndToken=tkBraceClose);
         for i:=OldCount to ARec.Members.Count-1 do
         for i:=OldCount to ARec.Members.Count-1 do