Browse Source

* Delphi allows TEnum.in

Michaël Van Canneyt 1 year ago
parent
commit
fc6fc67295
2 changed files with 39 additions and 2 deletions
  1. 15 1
      packages/fcl-passrc/src/pparser.pp
  2. 24 1
      packages/fcl-passrc/tests/tcexprparser.pas

+ 15 - 1
packages/fcl-passrc/src/pparser.pp

@@ -2627,6 +2627,8 @@ var
   SrcPos, ScrPos: TPasSourcePos;
   ProcType: TProcType;
   ProcExpr: TProcedureExpr;
+  AllowKWAsSubIdent : Boolean;
+
 begin
   Result:=nil;
   CanSpecialize:=aCannot;
@@ -2723,7 +2725,7 @@ begin
   else
     ParseExcExpectedIdentifier;
   end;
-
+  AllowKWAsSubIdent:=(msDelphi in CurrentModeswitches);
   Result:=Last;
   ISE:=nil;
   NextToken;
@@ -2754,6 +2756,17 @@ begin
         Func:=Expr;
         NextToken;
         end
+      else if AllowKWAsSubIdent and (Curtoken>=tkabsolute) and (Curtoken<=tkXor) then
+        begin
+        // Delphi allows keywords as identifier e.g. TEnum.In, but only for enums.
+        // Unfortunately, we do not know at this point if the previous identifier is an enum, so we allow it always.
+        // Not ideal :/
+        aName:=aName+'.'+CurTokenString;
+        Expr:=CreatePrimitiveExpr(AParent,pekIdent,CurTokenString);
+        AddToBinaryExprChain(Result,Expr,eopSubIdent,ScrPos);
+        Func:=Expr;
+        NextToken;
+        end
       else
         begin
         UngetToken;
@@ -2947,6 +2960,7 @@ const
 Var
   AllowedBinaryOps : Set of TToken;
   SrcPos: TPasSourcePos;
+
 begin
   AllowedBinaryOps:=BinaryOP;
   if Not AllowEqual then

+ 24 - 1
packages/fcl-passrc/tests/tcexprparser.pas

@@ -5,7 +5,7 @@ unit tcexprparser;
 interface
 
 uses
-  Classes, SysUtils, fpcunit, testregistry, tcbaseparser, pastree, PScanner;
+  Classes, SysUtils, fpcunit, testregistry, tcbaseparser, pastree, pparser, PScanner;
 
 type
 
@@ -110,6 +110,8 @@ type
     Procedure TestAPlusBBracketDotC;
     Procedure TestADotBDotC;
     Procedure TestADotBBracketC;
+    procedure TestADotKeyWord;
+    procedure TestADotKeyWordOnlyDelphi;
     Procedure TestSelfDotBBracketC;
     Procedure TestAasBDotCBracketFuncParams;
     Procedure TestRange;
@@ -1236,6 +1238,27 @@ begin
   AssertExpression('right b',PlusB.Right,pekIdent,'b');
 end;
 
+procedure TTestExpressions.TestADotKeyWord;
+
+begin
+  Add('{$MODE DELPHI}');
+  Add('Type TEnum = (&in,&of);');
+  Add('Var a : TEnum;');
+  Add('begin');
+  Add('  a:=Tenum.in;');
+  ParseExpression;
+  AssertExpression('Binary identifier',TheExpr,pekBinary,TBinaryExpr);
+end;
+
+procedure TTestExpressions.TestADotKeyWordOnlyDelphi;
+begin
+  Add('Type TEnum = (&in,&of);');
+  Add('Var a : TEnum;');
+  Add('begin');
+  Add('  a:=Tenum.in;');
+  AssertException(EParserError,@ParseExpression);
+end;
+
 procedure TTestExpressions.TestADotBDotC;
 var
   B, SubB: TBinaryExpr;