diff --git a/packages/fcl-passrc/src/pparser.pp b/packages/fcl-passrc/src/pparser.pp index a888682e5d..b0899dce5a 100644 --- a/packages/fcl-passrc/src/pparser.pp +++ b/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 diff --git a/packages/fcl-passrc/tests/tcexprparser.pas b/packages/fcl-passrc/tests/tcexprparser.pas index 3e0c57c461..5bbe2996b1 100644 --- a/packages/fcl-passrc/tests/tcexprparser.pas +++ b/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;